package cn.edu.swpu.sims.controller;

import cn.edu.swpu.sims.model.Admin;
import cn.edu.swpu.sims.service.AdminService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author 码之泪殇
 * @date 2020/9/11 10:35
 * 到达胜利之前，无法回头
 */
@Controller
@Slf4j
@RequestMapping("/admin")
public class AdminController {

    @Autowired
    private AdminService adminService;

    @PostMapping("/login")
    public ModelAndView login(String userName, String password, String userType, HttpServletRequest request) {
        ModelAndView mv = new ModelAndView();
        if (userName == null || "".equals(userName.trim())) {
            mv.setViewName("login");
            mv.addObject("msg","用户名不能为空");
            return mv;
        }
        log.info("登录信息:[userName:{}, password:{}, userType:{}]",userName,password,userType);
        // 验证用户
        Admin admin = adminService.getAdminByUsername(userName);
        if (null == admin || !password.equals(admin.getPassword())) {
            mv.setViewName("login");
            mv.addObject("msg","用户名或密码错误");
            return mv;
        }
        mv.setViewName("mainAdmin");
        request.getSession(true).setAttribute("admin",admin);
        mv.addObject("mainPage","admin/blank.jsp");
        return mv;
    }

    @GetMapping("/blank")
    public ModelAndView blank() {
        ModelAndView mv = new ModelAndView("mainAdmin");
        mv.addObject("mainPage","admin/blank.jsp");
        return mv;
    }

    @GetMapping("/info")
    public ModelAndView adminInfo() {
        ModelAndView mv = new ModelAndView("mainAdmin");
        mv.addObject("mainPage","admin/adminInfo.jsp");
        return mv;
    }

    @GetMapping("/logout")
    public ModelAndView logout(HttpServletRequest request) {
        // 清除admin登录信息
        request.getSession().removeAttribute("admin");
        return new ModelAndView("login");
    }

    @GetMapping("/show")
    public ModelAndView adminList(@RequestParam(value = "searchType", defaultValue = "username") String searchType, String s_Text) {
        ModelAndView mv = new ModelAndView("mainAdmin");
        log.info(s_Text);
        List<Admin> adminList = adminService.getAllAdmin(searchType,s_Text);
        mv.addObject("adminList",adminList);
        mv.addObject("mainPage","admin/adminList.jsp");
        return mv;
    }

    @GetMapping("/add")
    public ModelAndView addAdminPage() {
        ModelAndView mv = new ModelAndView("mainAdmin");
        mv.addObject("mainPage","admin/adminAdd.jsp");
        return mv;
    }

    @PostMapping("/add")
    public ModelAndView addAdmin(Admin admin) {
        ModelAndView mv = new ModelAndView("mainAdmin");
        Admin adminByUsername = adminService.getAdminByUsername(admin.getUsername());
        if (adminByUsername != null) {
            mv.addObject("mainPage","admin/adminAdd.jsp");
            mv.addObject("msg","管理员"+adminByUsername.getUsername()+"已存在,请勿重复创建");
            return mv;
        }
        adminService.saveAdmin(admin);
        mv.addObject("mainPage","admin/adminAdd.jsp");
        mv.addObject("msg","管理员添加成功");
        return mv;
    }

    @GetMapping("/del/{id}")
    public ModelAndView delAdmin(@PathVariable int id) {
        adminService.deleteAdminById(id);
        return adminList(null,null);
    }

    @GetMapping("/edit/{id}")
    public ModelAndView editAdminPage(@PathVariable int id) {
        ModelAndView mv = new ModelAndView("mainAdmin");
        Admin adm = adminService.getAdminById(id);
        mv.addObject("adm",adm);
        mv.addObject("mainPage","admin/adminEdit.jsp");
        return mv;
    }

    @PostMapping("/edit/{id}")
    public ModelAndView editAdmin(Admin admin,@PathVariable int id) {
        ModelAndView mv = new ModelAndView("mainAdmin");
        admin.setId(id);
        int res = adminService.updateAdmin(admin);
        if (res > 0) {
            mv.addObject("msg","管理员信息更新成功");
        } else {
            mv.addObject("msg","管理员信息更新失败");
        }
        Admin adm = adminService.getAdminById(id);
        mv.addObject("adm",adm);
        mv.addObject("mainPage","admin/adminEdit.jsp");
        return mv;
    }
}
